home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / EXAMPLES / SIMPLE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  2.2 KB  |  63 lines

  1.  
  2. /* Copyright (c) Mark J. Kilgard, 1996. */
  3.  
  4. /* This program is freely distributable without licensing fees 
  5.    and is provided without guarantee or warrantee expressed or 
  6.    implied. This program is -not- in the public domain. */
  7.  
  8. /* This program is a response to a question posed by Gil Colgate
  9.    <gcolgate@sirius.com> about how lengthy a program is required using
  10.    OpenGL compared to using  Direct3D immediate mode to "draw a
  11.    triangle at screen coordinates 0,0, to 200,200 to 20,200, and I
  12.    want it to be blue at the top vertex, red at the left vertex, and
  13.    green at the right vertex".  I'm not sure how long the Direct3D
  14.    program is; Gil has used Direct3D and his guess is "about 3000
  15.    lines of code". */
  16.  
  17. /* X compile line: cc -o simple simple.c -lglut -lGLU -lGL -lXmu -lXext -lX11 -lm */
  18.  
  19. #include <GL/glut.h>
  20.  
  21. void
  22. reshape(int w, int h)
  23. {
  24.   /* Because Gil specified "screen coordinates" (presumably with an
  25.      upper-left origin), this short bit of code sets up the coordinate
  26.      system to correspond to actual window coodrinates.  This code
  27.      wouldn't be required if you chose a (more typical in 3D) abstract
  28.      coordinate system. */
  29.  
  30.   glViewport(0, 0, w, h);       /* Establish viewing area to cover entire window. */
  31.   glMatrixMode(GL_PROJECTION);  /* Start modifying the projection matrix. */
  32.   glLoadIdentity();             /* Reset project matrix. */
  33.   glOrtho(0, w, 0, h, -1, 1);   /* Map abstract coords directly to window coords. */
  34.   glScalef(1, -1, 1);           /* Invert Y axis so increasing Y goes down. */
  35.   glTranslatef(0, -h, 0);       /* Shift origin up to upper-left corner. */
  36. }
  37.  
  38. void
  39. display(void)
  40. {
  41.   glClear(GL_COLOR_BUFFER_BIT);
  42.   glBegin(GL_TRIANGLES);
  43.     glColor3f(0.0, 0.0, 1.0);  /* blue */
  44.     glVertex2i(0, 0);
  45.     glColor3f(0.0, 1.0, 0.0);  /* green */
  46.     glVertex2i(200, 200);
  47.     glColor3f(1.0, 0.0, 0.0);  /* red */
  48.     glVertex2i(20, 200);
  49.   glEnd();
  50.   glFlush();  /* Single buffered, so needs a flush. */
  51. }
  52.  
  53. int
  54. main(int argc, char **argv)
  55. {
  56.   glutInit(&argc, argv);
  57.   glutCreateWindow("single triangle");
  58.   glutDisplayFunc(display);
  59.   glutReshapeFunc(reshape);
  60.   glutMainLoop();
  61.   return 0;             /* ANSI C requires main to return int. */
  62. }
  63.